<?php
// $Id: dish.php 430 2012-06-03 04:08:38Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Dish_Action extends Action_Admin{

    public function init(){
        parent::init();

        $this->shop_id = intval($this->params);
        $this->setVar('shop_id', $this->shop_id);

        $this->dish = App::module('table_dish', 'table', array($this->user));
        if(!$this->dish->check_user_right($this->shop_id)){
            $this->renderDialog('您无权管理此菜品信息！');
        }
    }

    public function onModify(){
        $this->setVar('show_validator', 1);

        list($shop_id, $dish_id) = explode('-', $this->params);
        $dish_id = intval($dish_id);

        $data = $this->dish->get_data($this->shop_id, $dish_id);
        $cats = $this->dish->get_array_cat($shop_id);

        if($data){
            $this->setVar('cats', $cats);
            $this->setVar('data', $data);
            $this->render('admin/dish/edit');
        }else{
            $this->renderDialog('外卖信息不存在，无法读取信息！');
        }
    }

    public function onPerform(){
        $this->setVar('show_validator', 1);

        $cats = $this->dish->get_array_cat($this->shop_id);
        $this->setVar('cats', $cats);
        $this->render('admin/dish/perform');
    }

    public function onImport(){
        $this->setVar('show_validator', 1);

        $data = array();
        $cats = $this->dish->get_array_cat($this->shop_id);
        $this->setVar('cats', $cats);
        if(array_get_bool($_POST, 'dosubmit')){
            $content = array_get_string($_POST, 'content');
            $lines  = explode("\n", $content);
            foreach($lines as $line){
                if(trim($line)){
                    @list($name, $price, $desc) = explode(',', $line, 3);
                    $data[] = array(
                        'name'  => trim($name),
                        'price' => doubleval($price),
                        'desc'  => trim($desc),
                    );
                }
            }
        }
        $this->setVar('data', $data);
        $this->render('admin/dish/import');
    }


    public function onList(){
        $cats = $this->dish->get_list_cat($this->shop_id);
        $list = $this->dish->get_list($this->shop_id, $cats);

        $this->setVar('cats', $cats);
        $this->setVar('list', $list);
        $this->render('admin/dish/list');
    }

    public function onDelete(){
        $this->dish->delete($this->shop_id, array_get_int($_GET, 'dish_id'));
        $this->renderDialog('删除成功！', '/admin/dish/list/'.$this->shop_id);
    }

    public function onSave(){
        if(array_get_bool($_POST, 'dosubmit')){
             $_POST['shop_id'] = $this->shop_id;
            $method  = array_get_string($_POST, 'method');

            switch($method){
                case 'modify':
                    $dish_id = array_get_int($_POST, 'dish_id');
                    $this->dish->update($this->shop_id, $dish_id, $_POST);
                    $this->renderDialog('修改成功！', '/admin/dish/list/'.$this->shop_id);
                break;
                case 'create':
                    $this->dish->insert($_POST);
                    $this->renderDialog('添加成功！', '/admin/dish/list/'.$this->shop_id);
                break;
                case 'import':
                    $this->dish->import($this->shop_id, $_POST);
                    $this->renderDialog('批量添加成功！', '/admin/dish/list/'.$this->shop_id, 'add');
                break;
            }
        }else{
            $this->renderDialog('无效的参数！');
        }
    }

    public function onSortlist(){
        if(array_get_bool($_POST, 'dosubmit')){
            $list = $this->dish->sort($this->shop_id, array_get($_POST, 'sorts'));
        }
        $list = $this->onList();
    }

    function __destruct(){}
}
?>